// pages/member/recharge.js
var req = require('../../interfaceImpl.js');
var config = require('../../interfaceConfig.js');
var app = getApp();
var vipList = [];
var id = '';
var that;
Page({
    onLoad: function (options) {
        id='';
        that = this;
        this.setUserInfo();
        console.log(app.globalData.loginuserinfo.balance);
        //TODO 请求会员充值接口
        return req.VipPayList(app.globalData.token).then(res => {
            console.log(res)
            vipList = res.data.data;
            this.setData({ vipList: vipList })
        })
    },
    godetail: function (event) {
        wx.navigateTo({
            url: 'recharge1?id=' + event.currentTarget.dataset.id
        })
    },
    radioChange: function (e) {
        id = e.detail.value;
    },
    pay: function () {
        if (id != '') {
            wx.showLoading({
                title: '请稍候', mask: true
            })
            return req.VipPay(id, app.globalData.token).then(res => {
                console.log(res)
                wx.hideLoading();
                if (res.data.Success) {
                    var pay = res.data
                    //发起支付   
                    var timeStamp = pay.TimeStamp;
                    var packages = pay.Package;
                    var nonceStr = pay.NonceStr;
                    var paySign = pay.PaySign;
                    var param = { "timeStamp": timeStamp, "package": packages, "paySign": paySign, "signType": "MD5", "nonceStr": nonceStr };
                    //调用支付
                    // pay(param)
                    console.log("支付")
                    wx.requestPayment({
                        timeStamp: param.timeStamp,
                        nonceStr: param.nonceStr,
                        package: param.package,
                        signType: param.signType,
                        paySign: param.paySign,
                        success: function (res) {
                            wx.showToast({
                                title: '充值成功',
                                icon: 'success',
                                duration: 2000,
                                complete: function () {
                                    setTimeout(function () {
                                        req.GetAndSetUserInfo();
                                        wx.navigateBack({ changed: true });
                                    }, 2000) //延迟时间 
                                }
                            })
                        },
                        fail: function (error) {
                            console.log(error);
                            if (error.errMsg=="requestPayment:fail cancel") {
                                console.log("用户取消");
                                return;
                            }
                            wx.showModal({
                                title: '充值失败',
                                content: "请重试",
                                showCancel: false,
                                success: function (res) {
                                }
                            })
                        }
                    })
                } else {
                    wx.showModal({
                        title: '请求失败',
                        content: res.data.data,
                        showCancel: false,
                        success: function (res) {
                        }
                    })
                }
            })
        } else {
            wx.showModal({
                title: '提示',
                showCancel: false,
                content: '请选择充值内容',
            })
        }
    },
    //设置用户余额等信息
    setUserInfo: function () {
        console.log("setUserInfo:");
        console.log(app.globalData.loginuserinfo);
        if (app.globalData.userinfo == '') {
            //头像 名字 余额 积分 
            this.setData({
                head: '../../resources/header_icon.png',
                name: app.globalData.loginuserinfo.mobile,
                money2: app.globalData.loginuserinfo.balance
            })
        }
        else {
            this.setData({
                head: app.globalData.userinfo.avatarUrl,
                name: app.globalData.userinfo.nickName,
                money2: app.globalData.loginuserinfo.balance,
                cardNum: app.globalData.loginuserinfo.cardNum,
                vipType: app.globalData.loginuserinfo.vipType
            })
        }
    }
})